package personal.zq.practice.common.utils;

import cn.org.bjca.security.SecurityUtil;
import cn.org.bjca.utils.Base64Util;
import cn.org.bjca.valuebean.BJCA_type;
import java.security.MessageDigest;

/**
 * 太保P13登录--密码加密专用
 */
public class PasswordUtil {
	
	private static Base64Util base64Util =new Base64Util("");;
	private static SecurityUtil securityUtil = SecurityUtil.getInstance();
	private static byte[] key = {-44, 99, 78, -12, -84, -124, -117, 79, -3, -40, -31, 116, 48, -125, -88, 72};

	//P13加密
	public static String getEncryptPwd(String pwd){
		byte[] encryptData = securityUtil.encryptData(key, encodePwdBySha(pwd).getBytes(), BJCA_type.BCA_ALGO_SM4_CBC, null);
		String encryptPwd=base64Util.BJS_EncodeBase64(encryptData);
		return encryptPwd;
	}
	
	private static String encodePwdBySha(String pwd) {
		String result = "";
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-256");
			byte[] b = md.digest(pwd.getBytes());
			for (int i = 0; i < b.length; i++) {
				String tmp = Integer.toHexString(b[i] & 0xFF);
				if (tmp.length() == 1) {
					result += "0" + tmp;
				} else {
					result += tmp;
				}
			}
		} catch (Exception e) {
			
		}
		return result;
	}
}
